<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\Room\StoreRoomRequest;
use App\Http\Requests\Room\UpdateRoomRequest;
use Illuminate\Http\Request;
use App\Model\Room;
class RoomController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xMem'=>'am-in',
            '_room'=>'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        
        $members=Room::orderBy('sort_order')->paginate(config('xSystem.page_size'));
        
        return view('Admin.xHotel.room.index')->with('members',$members);
    }

    /**
     * 新增显示页
     */
    public function create(){

        return view('Admin.xHotel.room.create');
    }

    /**
     * 新增
     * @param StoreRoomRequest $request
     */
    public function store(StoreRoomRequest $request)
    {

        Room::create($request->all());
        return redirect('/room')->with('success', '添加成功');

    }

    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $room = Room::find($id);
        return view('Admin.xHotel.room.edit')->with('room',$room);
    }

    /**
     * 修改
     * @param UpdateRoomRequest $request
     * @param $id
     */
    public function update(UpdateRoomRequest $request, $id)
    {
        $room = Room::find($id);
        $room->update($request->all());
        return redirect('/room')->with('success', '编辑成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {
        if (!Room::is_member($id)) {
            return back()->with('error', '当前房间有入住用户，请先退房后再尝试删除~');
        }
        Room::destroy($id);
        return back()->with('success', '删除成功');

    }

    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request){
       
        $sort_order=$request->sort_order;
        $category = Room::find($request->id);
        $category->sort_order=$sort_order;
        $category->save();
    }


}
